﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

using Newtonsoft.Json.Linq;

namespace KorbeRTR.MES
{
    public class InfoBeanHelper
    {
        public const string Prefix = "HJ-KP-";

        [InfoBean(Prefix + "0001", "宽度", "mm", "板材宽度")]
        public double K宽度;

        [InfoBean(Prefix + "0002", "高度", "mm", "板材高度")]
        public double G高度;

        [InfoBean(Prefix + "0003", "浮板高度", "mm")]
        public double F浮板高度;

        [InfoBean(Prefix + "0004", "电镀效率", "")]
        public double D电镀效率;

        /// <summary>
        /// 获取Json
        /// </summary>
        public JArray ToJson()
        {
            var array = new JArray();

            var fields = typeof(InfoBeanHelper).GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
            foreach (var iter in fields)
            {
                var attr = iter.GetCustomAttribute<InfoBeanAttribute>();
                if (attr == null)
                {
                    continue;
                }

                var obj = new JObject
                {
                    ["InfoID"] = attr.InfoID,
                    ["InfoName"] = attr.InfoName,
                    ["Uint"] = attr.Uint,
                };

                switch (iter.GetValue(this))
                {
                    case bool tmp:
                        obj["NowValue"] = tmp;
                        break;

                    case int tmp:
                        obj["NowValue"] = tmp;
                        break;

                    case double tmp:
                        obj["NowValue"] = tmp;
                        break;

                    case string tmp:
                        obj["NowValue"] = tmp;
                        break;
                }

                array.Add(obj);
            }

            return array;
        }

        /// <summary>
        /// 获取注册表
        /// </summary>
        public static JArray GetRegistry()
        {
            var array = new JArray();

            var fields = typeof(InfoBeanHelper).GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
            foreach(var iter in fields)
            {
                var attr = iter.GetCustomAttribute<InfoBeanAttribute>();
                if (attr == null)
                {
                    continue;
                }

                var obj = new JObject
                {
                    ["InfoID"] = attr.InfoID,
                    ["InfoName"] = attr.InfoName,
                    ["Uint"] = attr.Uint,
                    ["Describe"] = attr.Describe
                };

                array.Add(obj);
            }

            return array;
        }
    }

    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
    public class InfoBeanAttribute : Attribute
    {
        public InfoBeanAttribute(string InfoID, string InfoName, string Unit = "", string Describe = "")
        {
            this.InfoID = InfoID;
            this.InfoName = InfoName;
            this.Uint = Unit;
            this.Describe = Describe;
        }

        public string InfoID;

        public string InfoName;

        public string Uint;

        public string Describe;
    }
}
